package KiemTra;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class BaiTap02 extends JFrame{

	Thread t1, t2, t3, t4;
	JPanel canvas = new JPanel();
	JButton
		b1 = new JButton("Bất đầu"), 
		b2 = new JButton("Tạm dừng"),
		b3 = new JButton("Tiếp tục"),
		b4 = new JButton("Kết thúc");
	
	public BaiTap02(){
		super("Bài 2");
		setSize(400, 450);
		setVisible(true);
		add(canvas, BorderLayout.CENTER);
		canvas.setBackground(Color.WHITE);
		
		JPanel panelButton = new JPanel();
		panelButton.setLayout(new FlowLayout());
		panelButton.add(b1);
		panelButton.add(b2);
		panelButton.add(b3);
		panelButton.add(b4);
		
		b1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				t1.start();
				t2.start();
				t3.start();
				t4.start();
			}
		});
		
		b2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				t1.suspend();
				t2.suspend();
				t3.suspend();
				t4.suspend();
			}
		});
		
		b3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				t1.resume();
				t2.resume();
				t3.resume();
				t4.resume();
			}
		});
		
		b4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				t1.stop();
				t2.stop();
				t3.stop();
				t4.stop();
				System.exit(0);
			}
		});
		
		add(panelButton, BorderLayout.SOUTH);
		t1 = new ThreadTop(canvas);
		t2 = new ThreadButtom(canvas);
		t3 = new ThreadLeft(canvas);
		t4 = new ThreadRight(canvas);
		
	}
	
	public static void main(String[] args) {
		new BaiTap02();

	}
}
